<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
use Tools\Category;
//规则表控制器
class AuthruleController extends CommonController{
	public function index(){
		$db=M('authrule');
		$auth=$db->order('sort')->select();
		$auth=Category::unlimitedForLevel($auth);
		$this->assign('auth',$auth);

        //权限菜单的显示与隐藏    
        $this->assign('authRule_addId',38);//添加
        $this->assign('authRule_addNextId',39);//添加下一级
        $this->assign('authRule_delId',42);//删除
        $this->assign('authRule_menuId',114);//菜单配置
        $this->assign('authRule_editId',40);//编辑
        $this->assign('authRule_statusId',115);//审核

		$this->display();
	}
    public function add(){
        if(!empty($_POST)){
            $db=D('authrule');
            if(!$db->create()) {
                $this->error($db->getError());
            }else{
                // 写入帐号数据
                if($result   =   $db->add()) {
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }
        }else{
        	$db=M('authrule');
        	$field=array('id','title','pid');
    		$auth=$db->order('sort')->field($field)->select();
    		$auth=Category::unlimitedForLevel($auth);
    		$this->assign('auth',$auth);
        	$this->pid=I('pid',0,intval);    	
        	$this->display();
        }
    }
    public function addNext(){
        if(!empty($_POST)){
            $db=D('authrule');
            if(!$db->create()) {
                $this->error($db->getError());
            }else{
                // 写入帐号数据
                if($result   =   $db->add()) {
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }

        }else{
    	$this->pid=I('pid',0,intval);    	
    	$this->display();
        }
    }

    public function delete(){
    	$id = $_GET['id'];
    	$db=D('authrule');
    	if($db->where(array('pid'=>$id))->find()){
    		$this->error('删除数据失败，存在下级规则！');
    	}else{			
			if ($db->delete ($id)) {
                    $this->success ('删除成功！');
                } else {
                    $this->error ('删除失败！');
            }
    	}
    }
    public function edit(){
        if(!empty($_POST)){
            $rule    =   D("authrule");
            if(!$rule->create()) {
                $this->error($rule->getError());
            }else{
                // 修改帐号数据
                if($result=$rule->save()) {
                    $this->success('修改成功！');
                }else{
                    $this->error('修改失败！');
                }
            }
        }else{
        	$id = $_GET['id'];
        	$this->ppid= $_GET['pid'];
        	$db=M('authrule');
        	$field=array('id','title','pid');
    		$auth=$db->order('sort')->field($field)->select();
    		$this->auth=Category::unlimitedForLevel($auth);
    		if(isset($id)){
    			$rule=$db->find($id);
    			if ($rule){
                    $this->rule=$rule;
                    $this->display();
                }else{
                    $this->error ('数据加载失败');
                }
    		}
        }

    }
    //菜单项编辑
    public function menu(){
        $id = $_GET['id'];
        $db=M('authrule');
        $field=array('id','menu','target','rel');
        $this->auth=$db->field($field)->find($id);
        $this->display(); 
    }
}